import React from "react";
import { DataAction, IProTableRef } from "vendor/components/pro-table-native";
import { getDictDataPageList, getDictDataInfo, createDictData, updateDictData, deleteDictData } from "../api";

export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>, dictType: string): DataAction => ({
  fetchList: (params) => {
    const { page, pageSize, filter } = params;
    return getDictDataPageList(Object.assign({ page, pageSize, dictType }, filter || {})).then((resp) => resp.data);
  },
  getItem: (rowKey) => {
    const dataSetId = rowKey as number;
    return getDictDataInfo(dataSetId).then((resp) => {
      const { dictLabel, dictValue, dictSort, status, remark , isDefault} = resp.data.dict;
      return { dictLabel, dictValue, dictSort, status, isDefault, remark, };
    });
  },
  createItem: (params) => {
    return createDictData({ ...params, dictType }).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  updateItem: (rowKey, params) => {
    return updateDictData(rowKey as number, { ...params, dictType }).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  deleteItems: (selectedRowKeys) => {
    return deleteDictData(selectedRowKeys.map((item) => parseInt(item)))
  },
})


